package com.licenta.fbApp.client;

import com.licenta.fbApp.client.AppEventBus;
import com.licenta.fbApp.client.DisplayUserInfo;
import com.licenta.fbApp.client.FriendClickedEvent;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;

public class RatePanel extends Composite {
	
  public static final String STAR_URL = "img/glowStar.png";
  public static final String NOSTAR_URL = "img/emptyStar.png";
  public static final String EMPTY_URL = "img/noStar.png";
  
  /**
 * @uml.property  name="panel"
 * @uml.associationEnd  multiplicity="(1 1)"
 */
final HorizontalPanel panel = new HorizontalPanel();
  /**
 * @uml.property  name="mAX_STAR"
 */
final int MAX_STAR = 8;
  /**
 * @uml.property  name="star"
 * @uml.associationEnd  multiplicity="(0 -1)"
 */
final Image[] star = new Image[MAX_STAR ];
  
  public RatePanel(String restaurantName) {
	  
    initWidget(panel);
    addStyleName("ratePanel");
    
    for ( int i = 0; i < MAX_STAR; i++) {
    	
    	if ( i == 0) {
    		star[0] = new Image(EMPTY_URL);
    	} else {    	
    		star[i] = new Image(NOSTAR_URL); 
    	}
    	
    	star[i].addMouseOverHandler(new MouseOverHandler(){
    		
			public void onMouseOver(MouseOverEvent event) {	
    			colorRate((Image) event.getSource());
			}
    	});
    	
    	star[i].addMouseOutHandler(new MouseOutHandler(){
    		
			public void onMouseOut(MouseOutEvent event) {
    			makeNull();
			}
    	});
    	star[i].addClickHandler(new ClickHandler() {	
	      
	      public void onClick(ClickEvent event) {
	        // when star clicked, new rate with value i+1
	        //AppEventBus.getEventBus().fireEvent(
	          //new FriendClickedEvent(DisplayUserInfo.getUid(), true));
	      }
	    });
    }
    
    // will auto-resize
    panel.setHeight("0px");
    panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
    for ( int i = 0; i < MAX_STAR; i++) {
    	panel.add(star[i]);
    }
  }
  
  private void makeNull() {
	  for (int i = 0; i < MAX_STAR; i++) {
		  star[i].setUrl(NOSTAR_URL);
	  }
	  star[0].setUrl(EMPTY_URL);
  }
  private void colorRate(Image source) {
	  int i, j;
 
	  for ( i = 0; i < MAX_STAR; i++) {
		  if (source.equals(star[i])) {
			  for ( j = 0; j < i; j++) {
				  star[j + 1].setUrl(STAR_URL);
			  }
			  for ( j = i; j < MAX_STAR - 1; j++) {
				  star[j + 1].setUrl(NOSTAR_URL);
			  }
			  break;
		  }
	  }
	  star[0].setUrl(EMPTY_URL);
  }
}
